package com.ycode.bang.anbang.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.text.TextUtils;
import android.util.Log;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.google.gson.Gson;
import com.ycode.bang.anbang.MyApp;
import com.ycode.bang.anbang.R;
import com.ycode.bang.anbang.baseui.BaseActivity;
import com.ycode.bang.anbang.network.FinalHttpUtil;
import com.ycode.bang.anbang.utils.RequestMapDev;
import com.ycode.bang.anbang.utils.ToastUtil;

import net.tsz.afinal.http.AjaxCallBack;
import net.tsz.afinal.http.AjaxParams;

import org.json.JSONObject;

import butterknife.ButterKnife;
import butterknife.InjectView;

public class FindPasswordActivity extends BaseActivity implements View.OnClickListener {
    @InjectView(R.id.title)
    TextView title;

    @InjectView(R.id.et_find_userid)
    EditText mEtFindUserid;
    @InjectView(R.id.btn_get)
    Button mBtnGet;
    @InjectView(R.id.et_find_code)
    EditText mEtFindCode;
    @InjectView(R.id.et_find_new_pass)
    EditText mEtFindNewPass;
    @InjectView(R.id.et_find_new_pass_check)
    EditText mEtFindNewPassCheck;
    @InjectView(R.id.btn_Ok)
    Button mBtnOk;

    private TimeCount time = new TimeCount(60000, 1000);
    public boolean isSend = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_find_password);
        ButterKnife.inject(this);
        mEtFindUserid.setText(MyApp.userNumber);
        initView();
    }

    @Override
    protected void getSuccessRequest(Object response, int flag) {

    }

    private void initView() {
        title.setText("修改密码");
        mBtnOk.setOnClickListener(this);
        mBtnGet.setOnClickListener(this);
        if (!TextUtils.isEmpty(mEtFindUserid.getText().toString().trim())) {
            mEtFindUserid.setEnabled(false);
        }
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == android.R.id.home) {
            finish();
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_get:
                if (TextUtils.isEmpty(mEtFindUserid.getText().toString().trim())) {
                    ToastUtil.show(this, "请输入账号！");
                    return;
                }
                if (isSend) {
                    time.start();//倒计时
                    mBtnGet.setBackgroundResource(R.drawable.botton_bg_dark);
                    getCode();
                }
                break;
            case R.id.btn_Ok:
                if (TextUtils.isEmpty(mEtFindUserid.getText().toString().trim())) {
                    ToastUtil.show(this, "请输入账号！");
                    return;
                }
                if (TextUtils.isEmpty(mEtFindCode.getText().toString().trim())) {
                    ToastUtil.show(this, "请输入验证码！");
                    return;
                }
                if (TextUtils.isEmpty(mEtFindNewPass.getText().toString().trim())) {
                    ToastUtil.show(this, "请输入密码！");
                    return;
                }
                if (TextUtils.isEmpty(mEtFindNewPassCheck.getText().toString().trim())) {
                    ToastUtil.show(this, "请再次输入密码！");
                    return;
                }
                if (!mEtFindNewPassCheck.getText().toString().trim().equals(mEtFindNewPass.getText().toString().trim())) {
                    ToastUtil.show(this, "两次密码不一样！");
                    return;
                }
                changePassword();
                break;
        }
    }

    /**
     * 倒计时器
     */
    class TimeCount extends CountDownTimer {
        public TimeCount(long millisInFuture, long countDownInterval) {
            super(millisInFuture, countDownInterval);
        }

        @Override
        public void onFinish() {
            mBtnGet.setText("获取验证码");
            isSend = true;
            mBtnGet.setBackgroundResource(R.drawable.botton_bg_blue2);
            //  mBtnCode.setClickable(true);
        }

        @Override
        public void onTick(long millisUntilFinished) {
            isSend = false;
            mBtnGet.setBackgroundResource(R.drawable.botton_bg_dark);
            //   mBtnCode.setClickable(false);
            mBtnGet.setText(millisUntilFinished / 1000 + "秒");
        }
    }

    private void getCode() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("phone", mEtFindUserid.getText().toString().trim());
        FinalHttpUtil.get(RequestMapDev.GetCode, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        Gson gson = new Gson();
                    } else {
                        ToastUtil.show(FindPasswordActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(FindPasswordActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
//                ToastUtil.show(FindPasswordActivity.this, strMsg);
            }
        });
    }

    private void changePassword() {
        AjaxParams mAjaxParam = new AjaxParams();
        mAjaxParam.put("username", mEtFindUserid.getText().toString().trim());
        mAjaxParam.put("password", mEtFindNewPass.getText().toString().trim());
        mAjaxParam.put("code", mEtFindCode.getText().toString().trim());
        FinalHttpUtil.get(RequestMapDev.changePassword, mAjaxParam, new AjaxCallBack<String>() {
            @Override
            public void onSuccess(String o) {
                super.onSuccess(o);
                //放置数据
                try {
                    JSONObject jsonObject = new JSONObject(o.toString());
                    String status = jsonObject.getString("status");
                    String msg = jsonObject.getString("message");
                    String data = jsonObject.getString("data");
                    if (status.equals("0")) {
                        Log.e("这个接口是成功的", o);
                        Gson gson = new Gson();
                        ToastUtil.show(FindPasswordActivity.this, "修改成功,请重新登录！");
                        Intent intent = new Intent(FindPasswordActivity.this, LoginActivity2.class);
                        startActivity(intent);
                    } else {
                        ToastUtil.show(FindPasswordActivity.this, msg.toString());
                    }
                } catch (Exception e) {
                    ToastUtil.show(FindPasswordActivity.this, e.toString());
                }
            }

            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                loginout_550(errorNo);
//                ToastUtil.show(FindPasswordActivity.this, strMsg);
            }
        });
    }
}
